$('#reset').click(function(){
    $('input').val('');
})
var originAddress = true;
var membershipdata=""
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
var pageData = {};
var strdata={};
var companyNamesss=""

$(function () {
    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    })
});


var arr1 = new Array(); //1级分类ID
var arr2 = new Array(); //2级分类ID
var arr3 = new Array(); //3级分类ID
var applcationRange; //适用范围 0:全部分类 1:指定分类 2:指定场景
var arr4 = new Array();//根据二级查询的一级分类ID 数组
var arr5 = new Array();//根据三级查询的二级分类ID 数组
var member_id = sessionStorage.getItem("member_id");
var company_info_id = sessionStorage.getItem("id");
init();
function init() {
    $.ajax({
        /*  root.member.getMemberDetailByMemberId*/
        url:root.member.getMemberDetailByMemberId,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        async:false,
        data: {
            member_id:member_id,
            company_info_id:company_info_id
        },
        success: function (data) {
            console.log(data);
            //描述
            if(data.memberInfo.groundPushName!=""&& data.memberInfo.groundPushName!=null){
                $("#companyn").css({
                    display: "block"
                });
            }
            $('#login_account').html(data.memberInfo.login_account);
            $('#mobile_phone').html(data.memberInfo.mobile_phone);
            $('#registerPlatform').html(data.memberInfo.register_platform);
            if(data.memberInfo.register_platform == 10){
                $('#registerPlatform').html('PC端');
            }else if (data.memberInfo.register_platform == 20) {
                $('#registerPlatform').html('Andriod端');

            }else if (data.memberInfo.register_platform == 30) {
                $('#registerPlatform').html('IOS端');

            }else if (data.memberInfo.register_platform == 40) {
                $('#registerPlatform').html('H5端');

            }else if (data.memberInfo.register_platform == 50) {
                $('#registerPlatform').html('云采后台');

            }else if (data.memberInfo.register_platform == 60) {
                $('#registerPlatform').html('小程序端');

            }else if (data.memberInfo.register_platform == 0) {
                $('#registerPlatform').html('');

            }
            if(data.customer!=null&&data.customer!=""){
                $('#belongGroupName').html(data.customer.belongGroupName);
                $('#username').html(data.customer.username);
                var priceTypes=""
                if(data.customer.priceType==10){
                    priceTypes="售价"
                }else if(data.customer.priceType==20){
                    priceTypes="现金价"
                }else if(data.customer.priceType==30){
                    priceTypes="现金价"
                }
                $('#priceType').html(priceTypes);
            }
            $('#register_time').html(data.memberInfo.register_time);
            $('#email').html(data.memberInfo.email);


            if (data.memberInfo.groundPushName!=''&&data.memberInfo.groundPushName!=null) {
                if(data.memberInfo.groundStatus==10){
                    $('#groundPushName').html(data.memberInfo.groundPushName);
                }else {
                    $('#groundPushName').html(data.memberInfo.groundPushName+'(关闭)');
                }

            }

            $('#register_ip').html(data.memberInfo.register_ip);
            $('#register_addr').html(data.memberInfo.register_addr);


            $('#customer_name').html(data.memberInfo.customer_name);
            $('#customer_code').html(data.memberInfo.customer_code);
            $('#create_user').html(data.memberInfo.create_user);

            //发票信息
            if (data.generalInvoiceInfo != null && data.generalInvoiceInfo != "") {
                $('#compfanyname').html(data.generalInvoiceInfo.company_name);
                $('#taxpayefr_code').html(data.generalInvoiceInfo.taxpayer_code);
                $('#General').removeAttr("style");
                $('#General').attr("onclick", "generalvote()");

            } else {
                $("#General").attr("style", "opacity: 0.2");
                $('#General').removeAttr('onclick');
            }
            if (data.varInvoiceInfo != null) {
                $('#companyname').html(data.varInvoiceInfo.company_name);
                $('#taxpayer_code').html(data.varInvoiceInfo.taxpayer_code);
                $('#reg_address').html(data.varInvoiceInfo.reg_address);
                $('#reg_telephone').html(data.varInvoiceInfo.reg_telephone);
                $('#open_account_bank').html(data.varInvoiceInfo.open_account_bank);
                $('#bank_account').html(data.varInvoiceInfo.bank_account);
                if (data.varInvoiceInfo.id != null) {
                    $('#Increaseticket').removeAttr("style");
                    $('#Increaseticket').attr("onclick", "invoice_edit()");

                } else {
                    $("#Increaseticket").attr("style", "opacity: 0.2");
                    $('#Increaseticket').removeAttr('onclick');
                }
            }

        },
        error: function () {
            alert("sfsg")
            $.Huimodalalert('失败！');
        }
    });
}
//客户基本信息编辑
function commom_edit() {
    $("#dizhi").modal("show");
    $(".addressTitle").html("客户基本信息更改");
    $("#companyNames").attr("disabled", "disabled");

    $("#qu").css({
        display: "none"
    });

    $.ajax({
        /*  root.member.getMemberDetailByMemberId*/
        url: root.member.getMemberDetailByMemberId,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        async:false,
        data: {
            member_id:member_id,
            company_info_id:company_info_id
        },
        success: function (data) {
            console.log(data);
            if(data.memberInfo.groundPushName!=""&& data.memberInfo.groundPushName!=null){
                $("#compaNames").css({
                    display: "block"
                });
            }
            $("#loginaccount").val(data.memberInfo.login_account);
            $("#mobilephone").val(data.memberInfo.mobile_phone);
            $("#register_Platform").val(data.memberInfo.register_platform);

            if(data.memberInfo.register_platform == 10){
                $("#register_Platform").val('PC端');
            }else if (data.memberInfo.register_platform == 20) {
                $("#register_Platform").val('Andriod端');

            }else if (data.memberInfo.register_platform == 30) {
                $("#register_Platform").val('IOS端');

            }else if (data.memberInfo.register_platform == 40) {
                $("#register_Platform").val('H5端');
            }else if (data.memberInfo.register_platform == 60) {
                $("#register_Platform").val('小程序端');
            }else if (data.memberInfo.register_platform == 0) {
                $("#register_Platform").val('');
            }
            $("#registertime").val(data.memberInfo.register_time);
            $("#status2").val(data.memberInfo.status);
            if(data.memberInfo.status == 10){
                $('#status2').val('开启');
            }else if (data.memberInfo.status == 20) {
                $('#status2').val('关闭');


            }else if (data.memberInfo.status == 30) {
                $('#status2').val('已注销');


            }
            $("#businessman").val(data.memberInfo.business_man);
            $("#businessphone").val(data.memberInfo.business_phone);
            if(data.memberInfo.email!=null){
                $("#email2").val(data.memberInfo.email);
            }else {
                $("#email2").val("");
            }


            if (data.memberInfo.groundPushName!=''&&data.memberInfo.groundPushName!=null) {
                if(data.memberInfo.groundStatus==10){
                    $('#loginaccounts').val(data.memberInfo.groundPushName);
                }else {
                    $('#loginaccounts').val(data.memberInfo.groundPushName+'(关闭)');
                }

            }

            if(data.memberInfo.customer_leader!=""){
                $('#customerleaders').val(data.memberInfo.customer_leader);
            }else {
                $('#customerleaders').val("");
            }

            if(data.memberInfo.company_name!=""){
                companyNamesss=data.memberInfo.company_name
                $('#companyNames').val(data.memberInfo.company_name);
                $('#companyNames').text(data.memberInfo.pushman_company_id);

            }else {
                $('#companyNames').val('');
            }
            $('#registerip').val(data.memberInfo.register_ip);
            $('#registeraddr').val(data.memberInfo.register_addr);
            if(data.memberInfo.bind_status==20){
                $("#xiu").css({
                    display: "none"
                });
            }else {
                $("#xiu").css({
                    display: "inline"
                });
            }
            if(data.memberInfo.price_type==10){
                $("#price_types2").prop("checked",true)
            }else if(data.memberInfo.price_type==20){
                $("#price_types1").prop("checked",true)
            }else {
                $("#price_types1").prop("checked",false)
                $("#price_types2").prop("checked",false)
            }


        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}


initAddress()
function initAddress() {
    console.log(123123123)
    //初始化 省
    var user_data = {
        level:101,
        range:10
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode == 0){
            var tab_data=data.list;
            var cer = '';
            if(tab_data == ''){
                cer = '<option value=""></option>'
                $("#geta").html(cer);
            }else{
                cer = "<option value=''>请选择</option>"
                for(var i=0; i<tab_data.length; i++){
                    cer += '<option value="'+tab_data[i].address_id+','+tab_data[i].code+'">'+tab_data[i].name+'</option>'
                }
                $("#geta").html(cer);
            }
        }
    };
    web.doAjax(root.member.getLowerAddress,'post',user_data, succCallBack);
    /*
        "http://192.168.0.32:7060/amembership/adminGetRegionAddress.jhtml"
    */

}

//地址
function discountdata (val, el) {
    if(el == 2){
        //市
        var special=$('#geta').val()
        var split = val.split(",");
        console.log(split)
        console.log(special)

        if(special==""){
            var cer="<option value=''>请选择</option>"
            $("#getb").html(cer);
        }else {

            var user_data = {
                level: 102,
                parentCode: split[1]


            }
            var succCallBack = function (data, status, response) {
                strdata=data;
                if(data.errorCode=="0"){
                    var tab_data=data.list;
                    if(tab_data==''){
                        cer+='<option value=""></option>'
                        $("#getb").html(cer);
                    }else{
                        var cer="<option value=''>请选择</option>"
                        for(var i=0; i<tab_data.length; i++){
                            cer+='<option value="'+tab_data[i].address_id+','+tab_data[i].code+'">'+tab_data[i].name+'</option>'
                        }
                        $("#getb").html(cer);
                    }
                }
            };
            if(split[1] == 1 || split[1] == 2|| split[1] == 3 || split[1] == 4|| split[1] == 17|| split[1] == 32|| split[1] == 42|| split[1] == 43){
                $("#getd").hide();
            }else {
                $("#getd").show();
            }
            if (split[1] ==84){

                $("#getc").hide();
                $("#getd").hide();
            }else {
                $("#getc").show();
                $("#getd").show();
            }
            web.doAjax(root.member.getLowerAddress,'post',user_data, succCallBack);
        }
    } else if (el == 3) {
        var special=$('#geta').val()
        var split = val.split(",");
        if(special==""){
            var cer="<option value=''>请选择</option>"
            $("#getc").html(cer);
        }else {
            var user_data = {
                level: 103,
                parentCode:split[1]
            }
            var succCallBack = function (data, status, response) {
                if(data.errorCode=="0"){
                    var tab_data=data.list;
                    if(tab_data==''){
                        cer+='<option value=""></option>'
                        $("#getc").html(cer);
                    }else{
                        var cer="<option value=''>请选择</option>"
                        for(var i=0; i<tab_data.length; i++){
                            cer+='<option value="'+tab_data[i].address_id+','+tab_data[i].code+'">'+tab_data[i].name+'</option>'
                        }
                        $("#getc").html(cer);
                    }
                }
            };
            web.doAjax(root.member.getLowerAddress,'post',user_data, succCallBack);
        }
    } else if (el == 4) {
        var special=$('#geta').val()
        var split = val.split(",");
        if(special==""){
            var cer="<option value=''>请选择</option>"
            $("#getd").html(cer);
        }else {
            var user_data = {
                level: 104,
                parentCode: split[1]
            }
            var succCallBack = function (data, status, response) {
                if(data.errorCode=="0"){
                    var tab_data=data.list;
                    if(tab_data==''){
                        cer+='<option value=""></option>'
                        $("#getd").html(cer);
                    }else{
                        var cer="<option value=''>请选择</option>"
                        for(var i=0; i<tab_data.length; i++){
                            cer+='<option value="'+tab_data[i].address_id+','+tab_data[i].code+'">'+tab_data[i].name+'</option>'
                        }
                        $("#getd").html(cer);
                    }
                }
            };
            web.doAjax(root.member.getLowerAddress,'post',user_data, succCallBack);
        }
    }
}
function relation(id,ce) {

    $("#companyNames").val(ce).text(id)
    $("#tuank").hide()
}


//修改邮箱
var index=0;
function modifymailbox() {
    var cu = layer.confirm('<textarea id="remarksr" style="width:200px;height:100px;resize: none;" maxlength="60" placeholder="请输入邮箱" ></textarea>', {offset:'230px',title:'提示',btn: ['确认', '取消']}, function(index){
        fini3()
        layer.close(cu);
    },function(){
        layer.close(cu);

    });
}
//第二个
function fini3(){
    var remarksr = $("#remarksr").val();
    $.ajax({
        url:root.inter.updateCustomerInfoByCode,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        async:false,
        data: {
            email:remarksr
        },
        success: function (data) {
            location.replace(location.href)
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })

    layer.close(index);
}
//编辑普票信息
function generalvote() {
    $("#generalvote").modal("show");
    $.ajax({
        url: root.member.getMemberDetailByMemberId,
        cache: false, //禁用缓存
        type: "POST",
        dataType: 'json',
        async: false,
        data: {
            member_id: member_id,
            company_info_id: company_info_id
        },
        success: function(data) {
            $("#unitname").val(data.generalInvoiceInfo.company_name);
            $("#identificationnumber").val(data.generalInvoiceInfo.taxpayer_code);

        },
        error: function() {
            $.Huimodalalert('失败！', 2000);
        }
    });
}
//普票信息确认
function Determine() {
    if ($("#unitname").val() == '') {
        $.Huimodalalert('单位名称不可为空！', 2000);
        return false;
    }
    if ($("#identificationnumber").val() == '') {
        $.Huimodalalert('纳税人识别号不可为空！', 2000);
        return false;
    }
    $.ajax({
        url: root.member.updateInvoiceInfoByCertified,
        cache: false, //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            "memberId": member_id,
            "companyName": $("#unitname").val(),
            "taxpayerCode": $("#identificationnumber").val(),
            "invoiceType": 10,
            "optUserId": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
            "optUserName": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
            "regAddress": "",
            "regTelephone": "",
            "openAccountBank": "",
            "bankAccount": ""
        }),
        success: function(data) {
            if (data.errorCode == 0) {
                $.Huimodalalert('编辑成功', 2000);
                $("#generalvote").modal("hide");
                init()
                adsfdvf();
            } else {
                $.Huimodalalert(data.msg, 2000);
            }
        },
        error: function() {
            $.Huimodalalert('失败！', 2000);
        }
    });
}
//普票信息取消
function close_generalvote() {
    $("#generalvote").modal("hide");
    $("#unitname").val("")
    $("#identificationnumber").val("")
}
//普票信息X
function guanbi2() {
    $("#generalvote").modal("hide");
    $("#unitname").val("")
    $("#identificationnumber").val("")
}
//增票信息编辑
function invoice_edit() {
    $("#invoice").modal("show");
    $.ajax({
        url: root.member.getMemberDetailByMemberId,
        cache: false, //禁用缓存
        type: "POST",
        dataType: 'json',
        async: false,
        data: {
            member_id: member_id,
            company_info_id: company_info_id
        },
        success: function(data) {
            $('#companyname2').val(data.varInvoiceInfo.company_name);
            $('#taxpayer_code2').val(data.varInvoiceInfo.taxpayer_code);
            $('#reg_address2').val(data.varInvoiceInfo.reg_address);
            $('#reg_telephone2').val(data.varInvoiceInfo.reg_telephone);
            $('#open_account_bank2').val(data.varInvoiceInfo.open_account_bank);
            $('#bank_account2').val(data.varInvoiceInfo.bank_account);

        },
        error: function() {
            $.Huimodalalert('失败！', 2000);
        }
    });

}
//增票信息保存
function savecommom_edit2() {
    if ($("#companyname2").val() == '') {
        $.Huimodalalert('单位名称不可为空！', 2000);
        return false;
    }
    if ($("#taxpayer_code2").val() == '') {
        $.Huimodalalert('纳税人识别号不可为空！', 2000);
        return false;
    }
    $.ajax({
        url: root.member.updateInvoiceInfoByCertified,
        cache: false, //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            "memberId": member_id,
            "companyName": $("#companyname2").val(),
            "taxpayerCode": $("#taxpayer_code2").val(),
            "invoiceType": 20,
            "optUserId": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
            "optUserName": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
            "regAddress": $('#reg_address2').val(),
            "regTelephone": $('#reg_telephone2').val(),
            "openAccountBank": $('#open_account_bank2').val(),
            "bankAccount": $('#bank_account2').val()
        }),
        success: function(data) {
            if (data.errorCode == 0) {
                $.Huimodalalert('编辑成功', 2000);
                $("#invoice").modal("hide");
                init()
                adsfdvf();
            } else {
                $.Huimodalalert(data.msg, 2000);
            }
        },
        error: function() {
            $.Huimodalalert('失败！', 2000);
        }
    });
}
//取消
function close_model() {
    $("#company").modal("hide");
    $("#invoice").modal("hide");
    $("#tuank").hide()

}